#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+3;
int n,m,T,st,num,a[N],ori[N],d[N],q[N];
int cur[4],op[N],f[N];
char opo;
int tp,la,k;
int win[N];
LL sum[N],ans,answer[N],G[N];
bool vis[N];
void clear(){
	memset(vis,0,sizeof vis);
	memset(win,0,sizeof win);
	memset(G,0,sizeof G);
	memset(answer,0,sizeof answer);
	memset(f,0,sizeof f);
}
int hh(int x){
	int u=__lg(x);
	if((1<<u)==x) return u;
	return u+1;
}
void work(int x){
	if(x>=(1<<(tp+1))) return;
	ans-=x-st+1;
	vis[x]=1;
	work(x*2);work(x*2+1);
}
void dfs(int u,int rt,bool id){
	if(u==0) return;
	if(win[u]==1) {ans-=rt-st+1;return;}
	if(win[u]==-1) {dfs(u>>1,rt,(u&1));return;};
	if(op[u]==0){
		if(a[rt]>=d[u]){
			work(u*2+1);
			win[u]=1;
			dfs(u>>1,rt,(u&1));
		}
		else{
			if(d[u]<=k) ans-=rt-st+1;
			else G[d[u]]+=rt-st+1;
			win[u]=-1;
			return;
		}
	}
	else{
		if(!id){
			f[u]=rt-st+1;
			dfs(u>>1,rt,(u&1));
		}
		else{
			if(a[rt]>=d[u]){
				ans-=f[u];
				dfs(u>>1,rt,(u&1));
			}
			else{ans-=rt-st+1;return;}
		}
	}
}
int main()
{
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
    scanf("%d%d",&n,&m);
    k=hh(n);tp=k;
    for(int i=1;i<=(1<<k);i++) sum[i]=sum[i-1]+i;
    for(int i=0;i<k;i++)
        for(int j=(1<<i);j<(1<<(i+1));j++) d[j]=k-i;
    st=1<<k;
    for(int i=st;i<st+n;i++) scanf("%d",&ori[i]);
    for(int i=1;i<=m;i++) scanf("%d",&q[i]);
    for(int i=k-1;i>=0;i--){
		int num=1<<i;
        for(int j=0;j<(1<<i);j++){
			cin>>opo;
            op[num++]=opo-'0';
		}
	}
    scanf("%d",&T);
    while(T--){
		clear();
		for(int i=0;i<4;i++) scanf("%d",&cur[i]);
		for(int i=st;i<st+n;i++) a[i]=ori[i]^cur[(i-st+1)%4];
		ans=1;
		la=0,k=0;
		for(int i=st;i<st+n;i++){
			k=hh(i-st+1);
			if(k>la) ans+=sum[1<<k]-sum[1<<(k-1)],ans-=G[k];
			if(!vis[i]) dfs(i>>1,i,(i&1));
			la=k;
			answer[i-st+1]=ans;
		}
		LL print=0;
		for(int i=1;i<=m;i++) print=print^(answer[q[i]]*i);
		printf("%lld\n",print);
	}
	return 0;
}
